

layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging','form','laytpl'], function() {
    var $ = layui.jquery,
        paging = layui.paging(),
        layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
        layer = layui.layer, //获取当前窗口的layer对象
        laytpl = layui.laytpl,
        form = layui.form();
    var teacher = []; //获取老师信息
    axios.post('/teacherInfo/findDetailBatchBySchoolIdCondition', {
        schoolid:admin.schoolid
    })
        .then(function (response) {
            console.log(response.data);
            for (var i = 0; i < response.data.data.length; i++) {
                teacher[response.data.data[i].id] = response.data.data[i].user.realname;
            }
            var vaildTeacher = response.data.data;
            var html = '';
            for (var i in vaildTeacher) {
                html += '<option value="' + vaildTeacher[i].teacherInfo.id + '">' + vaildTeacher[i].user.realname + '</option>';
            }
            $('select[name=teacher]').append(html);
            form.render('select');
        })
        .catch(function (response) {
            console.log(response);
        });

    var enterprise = []; //获取企业信息
    axios.get('/orginfo/findAll')
        .then(function (response) {
            console.log(response.data);
            for (var i = 0; i < response.data.data.length; i++) {
                enterprise[response.data.data[i].id] = response.data.data[i].fullname;
            }
            var vaildEnterprise = response.data.data;
            var html = '';
            for (var i in vaildEnterprise) {
                html += '<option value=' + vaildEnterprise[i].id + '>' + vaildEnterprise[i].fullname+ '</option>';
            }
            $('select[name=intern]').append(html);
            form.render('select');
        })
        .catch(function (response) {
            console.log(response);
        });


    var linkage = {};
    //渲染下拉框
    linkage.formRender = function (obj, tip) {
        $(obj).empty();
        $(obj).append('<option value="">' + tip + '</option>');
        form.render('select');
    };
    // 初始无数据存在，不可进行更改，只赋值
    linkage.read = function (obj, url) {
        axios.get(url)
            .then(function (res) {
                var array = res.data.data;
                var html = '';
                for (var i in array) {
                    html += '<option value=' + array[i].id + '>' + array[i].name + '</option>';
                }
                $(obj).append(html);
                form.render('select');
                // console.log(init);
                // if (init)
                //     $(obj).next().find('[lay-value="' + id + '"]').click();
            })
            .catch(function (res) {
                console.log(res);
            });
    };

    var department = 'form select[name=department]',
        major = 'form select[name=major]',
        clazz = 'form select[name=clazz]';


    if($(department).length){
        linkage.formRender(department,'选择系');
        linkage.read(department,'/department/findBath/'+admin.schoolid);
        if($(major).length){
            linkage.formRender(major,'选择专业');
            form.on('select(department)',function (data) {
                // console.log(data);
                linkage.formRender(major,'选择专业');
                linkage.formRender(clazz,'选择班级');
                linkage.read(major,'/major/findBath/'+data.value);
            });
            if($(clazz).length){
                linkage.formRender(clazz,'选择班级');
                form.on('select(major)',function (data) {
                    // console.log(data);
                    linkage.formRender(clazz,'选择班级');
                    linkage.read(clazz,'/class/findBatchByMajorid/'+data.value);
                });
            }
        }
    }


    var vm = new Vue({
        el:'#Vue',
        data:{
            show:true,
            studentShow:false,
            interninfoShow:false,
            interninfo:{
                publisherid:publisherid
            },
            studentInfoDetail:{
                user:{
                    realname:""
                },
                major:{
                },
                clazz:{
                },
                studentInfo:{
                    state:""
                }
            },
            user: {
            }
            , studentInfo: {}
            , university: {}
            , school: {}
            , department: {}
            , major: {}
            , clazz: {}
            , teacherInfoInternName: ''
            , teacherInfoPaperName: ''
            , internInfoDetail:{},
            orgInfo:{

            }
        },
        methods:{
            /**
             * 筛选按钮
             */
            selectState:function () {
                var state = $('#state').val();
                var department = $('#department').val();
                var major = $('#major').val();
                var clazz = $('#clazz').val();
                var teacher = $('#teacher').val();
                console.log(teacher);
                paging.get({departmentid:department,majorid:major,classid:clazz,
                    state:state,schoolid:admin.schoolid,internadvisor:teacher
            });
            },
            /**
             * 清除筛选按钮
             */
            removeSelectState:function () {
                form.render('select');
                paging.get({departmentid:'',majorid:'',classid:'',state:'',internadvisor:'',schoolid:admin.schoolid});
                linkage.formRender(major,'选择专业');
                linkage.formRender(clazz,'选择班级');
            },
            pass:function () {
                top.layer.confirm('确认通过吗？',function (index) {
                    vm.interninfo.state = 5;
                    vm.studentInfoDetail.studentInfo.state = 3;
                    vm.interninfo.orgattr = 2;
                    axios.post('/student/modifyOneById',vm.studentInfoDetail.studentInfo)
                        .then(function (response) {

                        })
                        .catch(function (msg) {
                            console.log(msg);
                        });
                    axios.post('/internInfo/modify', vm.interninfo)
                        .then(function (res) {
                            layer.msg("操作成功", {time: 1500}, function () {
                                top.layer.close(index);
                                vm.reload();
                            })
                        })
                        .catch(function (res) {
                            layer.msg("程序出错");
                        });
                });
            },
            refuse:function () {
                layer.open({
                        type:0,
                        title:"审核意见",
                        content:laytpl($('#auditsugtpl').html()).render(vm.interninfo),
                        shade:[0.5,'#FFF'],
                        area:['700px','220px'],
                        zIndex: 19950924,
                        btn: ['提交'],
                        yes:function (index) {
                            top.layer.confirm("确认不通过吗？",function (index) {
                                vm.interninfo.auditsug = $("#auditsug").val();
                                vm.interninfo.state = 6;
                                vm.studentInfoDetail.studentInfo.state = 1;
                                axios.post('/student/modifyOneById', vm.studentInfoDetail.studentInfo)
                                    .then(function (response) {

                                    })
                                    .catch(function (msg) {
                                        console.log(msg);
                                    });
                                axios.post('/internInfo/modify/', vm.interninfo)
                                    .then(function (res) {
                                        layer.msg("操作成功", {time: 1500}, function () {
                                            vm.reload();
                                            top.layer.close(index);
                                        })
                                    })
                                    .catch(function (res) {
                                        layer.msg("程序出错");
                                    });
                            });
                            layer.close(index);

                        }
                    });
            },
            /**
             * 返回按钮
             */
            reload:function () {
                vm.show = true;
                vm.studentShow=false;
                vm.interninfoShow=false;
                paging.init();
                // $('#content input[type!=button]').attr("readOnly",true).addClass("disabled");
                // vm.getInfo(vm.interninfo.publisherid);
            }

        }
    });
    paging.init({
        openWait: true,
        url:'/internInfo/findBatchByCondition2',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
            schoolid:admin.schoolid
        },
        contentType : "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function(msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function() { //完成的回调
            //alert('处理完成');

            //绑定所有查看详情和审核按钮事件
            $('#content').children('tr').each(function() {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var id = $that.children('a[data-opt=more]').data('id');    //实习信息ID
                var studentUserId = $that.children('a[data-opt=mores]').data('id');
                if($that.children('a[data-opt=internChange]')) {
                    var studentInfoId_internChange = $that.children('a[data-opt=internChange]').data('id');
                }

                console.log(id);

                $that.children('a[data-opt=mores]').on('click', function () {
                    axios.get('/student/findDetail/' + studentUserId)
                        .then(function (Response) {
                            console.log(Response.data.data);
                            vm.user = Response.data.data.user;
                            vm.studentInfo = Response.data.data.studentInfo;
                            vm.university = Response.data.data.university;
                            vm.school = Response.data.data.school;
                            vm.department = Response.data.data.department;
                            vm.major = Response.data.data.major;
                            vm.teacherInfoInternName = Response.data.data.teacherInfoInternName;
                            vm.teacherInfoPaperName = Response.data.data.teacherInfoPaperName;
                            vm.clazz = Response.data.data.clazz;
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                    vm.show=false;
                    vm.studentShow=true;
                    vm.interninfoShow=false;
                });
                //学生详情
                $that.children('a[data-opt=more]').on('click', function() {
                    axios.get('/internInfo/findOne/'+id)
                        .then(function (response) {
                            console.log(response.data.data);
                            vm.studentInfoDetail.studentInfo = response.data.data.studentInfoDetail.studentInfo;
                            vm.interninfo = response.data.data.internInfo;
                            if(response.data.data.studentInfoDetail.user.realname!=""){
                            vm.studentInfoDetail.user.realname=response.data.data.studentInfoDetail.user.realname;}
                            if(response.data.data.studentInfoDetail.major!=null){
                            vm.studentInfoDetail.major=response.data.data.studentInfoDetail.major;}
                            if(response.data.data.studentInfoDetail.clazz!=null){
                            vm.studentInfoDetail.clazz=response.data.data.studentInfoDetail.clazz;}
                            if(response.data.data.orgInfo!=null){
                            vm.orgInfo=response.data.data.orgInfo;}
                            // console.log( vm.studentInfoDetail.user.realname);
                            // console.log( vm.studentInfoDetail.major);
                            // console.log(vm.studentInfoDetail.clazz);
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                    vm.show=false;
                        vm.studentShow=false;
                        vm.interninfoShow=true;

                });
                //实习详情和审核
                $that.children('a[data-opt=internChange]').on('click',function () {
                    location.href="/admin/internChangeReview?studentInfoId="+studentInfoId_internChange+'&studentUserId='+studentUserId;
                })
                //实习变更审核
            });

        }
    });





});